//
//  XFXTopicDetaViewController.m
//  XFXnews
//
//  Created by dllo on 16/1/30.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "XFXTopicDetaViewController.h"
#import "XFXTopicDetaTableViewCell.h"
#import "XFXTopicDetaHeaderModel.h"
#import "XFXTopicHeaderView.h"
#import "XFXTopicDeatCellModel.h"

@interface XFXTopicDetaViewController ()
<
    UITableViewDataSource,
    UITableViewDelegate

>

@property (nonatomic, strong)UITableView *myTableView;
@property (nonatomic, strong)NSMutableArray *allArray;
@property (nonatomic, strong)XFXTopicHeaderView *myView;

@end

@implementation XFXTopicDetaViewController


- (void)viewDidLoad {
    [super viewDidLoad];
  
    self.edgesForExtendedLayout = UIRectEdgeNone;
    self.automaticallyAdjustsScrollViewInsets = NO;
    
    self.navigationController.navigationBar.barTintColor = [UIColor redColor];
    self.navigationItem.title = @"详情";
    self.allArray = [NSMutableArray array];
    
    self.myView = [[XFXTopicHeaderView alloc]initWithFrame:CGRectMake(0, 0, XFXWIDTH, 250*XFXHEIGHT/736)];
    [self.view addSubview:self.myView];
    
    self.myTableView = [[UITableView alloc]initWithFrame:CGRectMake(0, 250*XFXHEIGHT/736, XFXWIDTH, XFXHEIGHT-(300*XFXHEIGHT/736)) style:UITableViewStyleGrouped];
    self.myTableView.delegate = self;
    self.myTableView.dataSource = self;
    [self.view addSubview:self.myTableView];
    
    
    [self getJson];
    
    UINib *nib = [UINib nibWithNibName:@"XFXTopicDetaTableViewCell" bundle:[NSBundle mainBundle]];
    [self.myTableView registerNib:nib forCellReuseIdentifier:@"cell"];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
  
    return self.allArray.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
 
    XFXTopicDetaTableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"cell"];
    cell.model = self.allArray[indexPath.row];
 
   
    [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
    return cell;
   
}

- (void)getJson{
    
    NSString *url = [NSString stringWithFormat:@"http://c.m.163.com/newstopic/qa/%@.html", self.model.expertId];
    [XFXAppTools getNewsWithHeadline:url block:^(id reulst) {
        NSMutableDictionary *dic = [reulst objectForKey:@"data"];
        NSMutableDictionary *dicc = [dic objectForKey:@"expert"];
        XFXTopicDetaHeaderModel *model = [[XFXTopicDetaHeaderModel alloc]init];
        [model setValuesForKeysWithDictionary:dicc];
        self.myView.model = model;
        
        NSMutableArray *array = [dic objectForKey:@"hotList"];
        for (NSMutableDictionary *dic in array) {
            XFXTopicDeatCellModel *model = [[XFXTopicDeatCellModel alloc]init];
            [model setValuesForKeysWithDictionary:dic];
            [self.allArray addObject:model];
      
        }
        
        [self.myTableView reloadData];
    } error:^(NSError *error) {
        
    }];
    
    
}

- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    
    UIView *myView = [[UIView alloc]initWithFrame:CGRectMake(0, 0, XFXWIDTH, 30*XFXHEIGHT/736)];
    UILabel *myLabel = [[UILabel alloc]initWithFrame:CGRectMake(30*XFXWIDTH/414, 10*XFXHEIGHT/736, 100*XFXWIDTH/414, 30*XFXHEIGHT/736)];
    myLabel.text = @"最热评论";
    myLabel.textColor = [UIColor redColor];
    [myView addSubview:myLabel];
    return myView;
    
}
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 1;
}

-(CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    return 50*XFXHEIGHT/736;
}



- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    return 300*XFXHEIGHT/736;
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
